﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Common.Entity.Item;
using System.Data.SqlClient;
using Hfm.Common.Constants.Item;

namespace Hfm.Core.DataAccess.ItemData
{
    public class ItemData
    {
        protected readonly CoreContext Context;

        public ItemData()
        {

        }

        public ItemData(CoreContext context)
        {
            Context = context;
        }

        public virtual List<ItemEntity> GetItemList()
        {
            var query = from i in Context.ItemEntities
                        select i;
            return query.ToList();
        }

        public virtual IList<NewItemEntity> GetAllItemes(string Name, string Code, string PartNumber, string Model, int Type = 0, int? skip = null, int? take = null)
        {
            if ((skip == null) ^ (take == null))
                throw new ArgumentException("Skip and take parameter must be provided together or left null.");

            var query = from ss in Context.ItemEntities
                        select new NewItemEntity
                        {
                            Country = ss == null ? 0 : ss.Country,
                            ItemCode = ss == null ? string.Empty : ss.ItemCode,
                            ItemDescription = ss == null ? string.Empty : ss.ItemDescription,
                            ItemId = ss == null ? 0 : ss.ItemId,
                            ItemName = ss == null ? string.Empty : ss.ItemName,
                            ItemType = ss == null ? 0 : ss.ItemType,
                            Manufacturer = ss == null ? string.Empty : ss.Manufacturer,
                            Model = ss == null ? string.Empty : ss.Model,
                            PartNumber = ss == null ? string.Empty : ss.PartNumber,
                            Price = ss == null ? 0 : ss.Price,
                            Remarks = ss == null ? string.Empty : ss.Remarks,
                            SupplierInformation = ss == null ? string.Empty : ss.SupplierInformation,
                            TrackSerialNumber = ss.TrackSerialNumber,
                            TrackStock = ss.TrackStock

                        };

            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.Where(ss => ss.ItemName.Contains(Name));
            }
            if (!string.IsNullOrWhiteSpace(Code))
            {
                query = query.Where(ss => ss.ItemCode.Contains(Code));
            }
            if (!string.IsNullOrWhiteSpace(PartNumber))
            {
                query = query.Where(ss => ss.PartNumber.Contains(PartNumber));
            }
            if (!string.IsNullOrWhiteSpace(Model))
            {
                query = query.Where(ss => ss.Model.Contains(Model));
            }
            if (Type > 0)
            {
                query = query.Where(ss => ss.ItemType.Equals(Type));
            }
            query = query.OrderBy(ss => ss.ItemId);

            if (skip != null && take != null)
            {
                query = query.Skip(skip ?? 0).Take(take ?? int.MaxValue);
            }

            IList<NewItemEntity> result = query.ToList();

            foreach (NewItemEntity newItemEntity in result)
            {
                string TypeName = "";
                if (ItemConstants.ItemType.TryGetValue(newItemEntity.ItemType, out TypeName))
                {
                    newItemEntity.ItemTypeName = TypeName;
                }
            }
            return result;
        }

        public virtual int SelectIsExitesCode(string Code, int Id)
        {
            var entity = Context.ItemEntities.Where(p => p.ItemCode == Code && p.ItemId != Id);
            return entity.Count();
        }

        public virtual int GetItemlist(string Name, string Code, string PartNumber, string Model, int Type = 0)
        {
            var query = from ss in Context.ItemEntities
                        select ss;

            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.Where(ss => ss.ItemName.Contains(Name));
            }
            if (!string.IsNullOrWhiteSpace(Code))
            {
                query = query.Where(ss => ss.ItemCode.Contains(Code));
            }
            if (!string.IsNullOrWhiteSpace(PartNumber))
            {
                query = query.Where(ss => ss.PartNumber.Contains(PartNumber));
            }
            if (!string.IsNullOrWhiteSpace(Model))
            {
                query = query.Where(ss => ss.Model.Contains(Model));
            }
            if (Type > 0)
            {
                query = query.Where(ss => ss.ItemType.Equals(Type));
            }
            query = query.OrderBy(ss => ss.ItemId);

            return query.Count();
        }

        public virtual ItemEntity GetItemById(int Id)
        {
            var item = Context.ItemEntities.FirstOrDefault(p => p.ItemId == Id);
            return item;
        }

        public virtual void AddItem(ItemEntity item)
        {
            Context.ItemEntities.Add(item);
            Context.SaveChanges();
        }

        public virtual void UpdateItem(ItemEntity item)
        {
            Context.ItemEntities.Attach(item);
            Context.Entry(item).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public virtual void DeleteItem(int Id)
        {
            string strSql = "DELETE FROM Item WHERE ItemId = @Id";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", Id));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }
    }
}
